package csc.fresher.customannotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import csc.fresher.bankservice.BankService;
import csc.fresher.controller.HomeController;
import csc.fresher.domain.UserInfo;

public class UniqueLoginIdValidator implements
		ConstraintValidator<UniqueLoginId, String> {

	@Autowired
	private BankService services;

	@Override
	public void initialize(UniqueLoginId constraintAnnotation) {
		// TODO Auto-generated method stub
		SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
	}

	@Override
	public boolean isValid(String loginId, ConstraintValidatorContext context) {
		System.out.println(loginId);
		if (HomeController.page.equals("create")) {
			if (services.checkLoginId(loginId)) {
				return false;
			} else {
				return true;
			}
		} 
		
		if (HomeController.page.equals("update") || HomeController.page.equals("profile") || HomeController.page.equals("changestatus")) {
			if (services.checkLoginId(loginId)) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

}
